import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { CacheService } from '../cache/cache.service';
import { ArticleService } from '../article/article.service';

//console.log(CronExpression);
@Injectable()
export class TasksService {
  constructor(
    private readonly cache: CacheService,
    private readonly articleService: ArticleService,
  ) {}
  @Cron(CronExpression.EVERY_HOUR) //'*/120 * * * * *' /*
  async SynchronousClick() {
    const data = await this.cache.general('hgetall', 'article_hit');
    if (JSON.stringify(data) !== '{}') {
      try {
        await this.articleService.syncData(data);
        await this.cache.general('del', 'article_hit');
        console.log('同步点击率成功！');
      } catch (e) {
        console.log('同步点击率失败！');
      }
    }
  }
}
